import entity.GroupMessage;
import enums.ResponseType;
import request.group.message.GroupMessagePullRequest;
import response.Response;
import response.group.message.GroupMessagePullResponse;

import javax.swing.*;
import java.awt.*;
import java.util.List;
import java.util.UUID;

public class GroupChatView extends JFrame {
    private DefaultListModel<GroupMessage> listModel;
    private SpringLayout springLayout = new SpringLayout();
    private JPanel panel = new JPanel(springLayout);
    private JScrollPane scrollPane = new JScrollPane();
    private JScrollPane groupMemberPane=new JScrollPane();
    private JButton sendButton = new JButton("发送");
    private JButton addMemberButton=new JButton("添加成员");
    private JButton quitGroupButton=new JButton("退出群聊");
    private JTextField textField = new JTextField();
    private GroupChatHandler groupChatHandler = new GroupChatHandler(this);

    private JList<GroupMessage> jList;

    private String hostUserName;
    private UUID uuid;

    public GroupChatView(String hostUserName,  List<GroupMessage> messageHistory, UUID uuid) {
        super("Now chatting in group:" + uuid);
        this.hostUserName = hostUserName;
        this.uuid=uuid;
        listModel = new DefaultListModel<>();
        for (GroupMessage message : messageHistory) {
            listModel.addElement(message);
        }
        jList = new JList<>(listModel);
        jList.setCellRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                GroupMessage message = (GroupMessage) value;
                String str = message.getSenderUsername() +  " 说: " + message.getContent();
                setText(str);
                return this;
            }
        });
        setBounds(500, 150, 800, 700);
        Font panelFont = new Font("楷体", Font.PLAIN, 18);
        sendButton.setFont(panelFont);
        addMemberButton.setFont(panelFont);
        quitGroupButton.setFont(panelFont);
        sendButton.addActionListener(groupChatHandler);
        addMemberButton.addActionListener(groupChatHandler);
        quitGroupButton.addActionListener(groupChatHandler);
        getRootPane().setDefaultButton(sendButton);
        textField.setFont(panelFont);
        jList.setFont(panelFont);
        textField.setPreferredSize(new Dimension(500, 30));
        scrollPane.setPreferredSize(new Dimension(781, 500));

        scrollPane.setViewportView(jList);
        panel.add(scrollPane);
        panel.add(sendButton);
        panel.add(textField);
        panel.add(addMemberButton);
        panel.add(quitGroupButton);


        springLayout.putConstraint(SpringLayout.NORTH, textField, 20, SpringLayout.SOUTH, scrollPane);
        springLayout.putConstraint(SpringLayout.WEST, textField, 50, SpringLayout.WEST, scrollPane);
        springLayout.putConstraint(SpringLayout.WEST, sendButton, 20, SpringLayout.EAST, textField);
        springLayout.putConstraint(SpringLayout.NORTH, sendButton, 0, SpringLayout.NORTH, textField);
        springLayout.putConstraint(SpringLayout.WEST,addMemberButton,20,SpringLayout.EAST,sendButton);
        springLayout.putConstraint(SpringLayout.NORTH,addMemberButton,0,SpringLayout.NORTH,sendButton);
        springLayout.putConstraint(SpringLayout.WEST,quitGroupButton,20,SpringLayout.EAST,addMemberButton);
        springLayout.putConstraint(SpringLayout.NORTH,quitGroupButton,0,SpringLayout.NORTH,addMemberButton);

        getContentPane().add(panel);
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);

        refreshChatView();
    }

    public void updateList(GroupMessage message) {
        listModel.addElement(message);
        jList.setModel(listModel);
    }

    public void updateList(List<GroupMessage> list) {
        if (list == null || list.isEmpty()) {
            return;
        }
        for (GroupMessage message : list) {
            listModel.addElement(message);
        }
        jList.setModel(listModel);
    }

    public void refreshChatView() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                GroupMessagePullRequest messagePullRequest = new GroupMessagePullRequest(hostUserName,uuid);
                Response response = ConnectionUtil.makeConnection(messagePullRequest);
                if (response.getType() == ResponseType.GroupMessagePull) {
                    GroupMessagePullResponse messagePullResponse = (GroupMessagePullResponse) response;
                    updateList(messagePullResponse.getGroupMessageList());
                }

            }
        }).start();
    }

    public JTextField getTextField() {
        return textField;
    }

    public String getHostUserName() {
        return hostUserName;
    }

    public UUID getUUID(){return uuid;};

}